package day3;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.zip.CRC32;

//模拟一个用户系统：序列化实战功能
//1.查询用户
//2.新增用户
//3.删除用户
//4.修改用户
//5.退出系统
//并且 程序关闭了 再启动还可以读取到之前存储的数据(数据库 序列化)
public class UserSystem {
    //保存所有注册的用户数据
    static List<User> users = new ArrayList<>();
    static Scanner sc = new Scanner(System.in);
    static {//类加载执行 而且执行一次
            //启动程序 反序列化 读取集合数据
        input();
    }
    //启动程序 需要做反序列化 读取集合数据 数据修改就需要保存新数据 序列化
    public static void main(String[] args) {
        menu();
        System.out.println("系统结束");
    }
    public static void menu(){
        while (true){
            System.out.println("某某某用户系统");
            System.out.println("**********");
            System.out.println("1.查询用户");
            System.out.println("2.新增用户");
            System.out.println("3.删除用户");
            System.out.println("4.修改用户");
            System.out.println("5.退出系统");
            System.out.println("**********");
            System.out.println("请输入你的选择:");
            int n = sc.nextInt();
            switch (n){
                case 1:
                    show();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    delete();
                    break;
                case 4:
                    update();
                    break;
                case 5:return;//结束当前方法
            }
        }
    }
    public static void show(){
        System.out.println("编号\t账号\t密码\t城市");
        for (int i = 0; i < users.size(); i++) {
            User u = users.get(i);
            System.out.println(i+1+"\t"+u.getUsername()+"\t"+u.getPassword()+"\t"+u.getCity());
        }
    }
    public static void add(){
        System.out.println("请输入账号：");
        String username = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();
        System.out.println("请输入城市：");
        String city = sc.next();
        users.add(new User(username,password,city));
        System.out.println("***"+username+"用户添加成功!");
        output();
    }
    public static void delete(){}
    public static void update(){}
    //反序列化 读取
    public static void input(){
        try (FileInputStream fis = new FileInputStream("d://IO//user.xxx");
             ObjectInputStream ois = new ObjectInputStream(fis)
        ){
            users =(List<User>)ois.readObject();
            System.out.println("集合加载成功");
        }catch (FileNotFoundException e){
            System.out.println("欢迎第一次使用!");
        }catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    //序列化  写入
    public static void output(){
        try (FileOutputStream fos = new FileOutputStream("d://IO//user.xxx");
             ObjectOutputStream oos = new ObjectOutputStream(fos);
        ){
            oos.writeObject(users);
            System.out.println("集合写入成功");
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
class User implements Serializable {
    private String username;
    private String password;
    private String city;

    public User(String username, String password, String city) {
        this.username = username;
        this.password = password;
        this.city = city;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}